<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<link rel="stylesheet" href="/com/ztree/zTreeStyle.css" />
<script src="/com/ztree/jquery.ztree.core.js"></script>
<script src="/com/ztree/jquery.ztree.excheck.js"></script>
<div class="bjui-pageContent" style="top: 0px; bottom: 32px;">
    <form id="role_add_menu_form_id" method="post" action="/admin/role/save">
            <input type="hidden" name="id" value="${roleId}">
            <input type="hidden" name="menuIdStr" id="edit_menus">
            <div style="height:500px;">
            <div class="row" style="margin-left: 50px!important;margin-right: 0px!important">
                <ul id="treeEdit" class="ztree"></ul>
            </div>
            </div>
    </form>
</div>
<div class="bjui-pageFooter">
    <ul>
        <li>
            <button type="button" class="btn btn-red cancel" onclick="BJUI.dialog('closeCurrent');" data-icon="remove">
                <i class="fa fa-remove"></i> 取消
            </button>
        </li>
        <li>
            <button type="button" class="btn btn-default save" onclick="doRoleAddMenus('treeEdit', $('#edit_menus'));" data-icon="save"><i class="fa fa-save"></i> 保存</button>
        </li>
    </ul>
</div>
<script>
    var roleId = '${roleId}';

    /**
     * 装载数据
     */
    $(function(){
        BJUI.ajax('doajax', {
            url: '/admin/role/roleMenu',
            data: {"id":roleId},
            loadingmask: true,
            callback: function(response, optiocns) {
                if (response.result) {
                    var setting = {
                        check: {
                            enable: true
                        },
                        data: {
                            simpleData: {
                                enable: true
                            }
                        }
                    };
                    var datas = response.list;
                    var nodes = getNodes(datas);
                    $.fn.zTree.init($("#treeEdit"), setting, nodes);
                } else {
                    BJUI.alertmsg('error', "服务繁忙，稍后重试", {
                        okCall: function () {
                            $('#role_table_cl').datagrid("refresh",true);
                        }
                    });
                }
            }
        });
    });

    /**
     * 树状图展示
     * @param datas
     * @returns {Array}
     */
    function getNodes(datas){
        var nodes = new Array();
        for (var i = 0; i < datas.length; i++) {
            var data = datas[i];
            nodes.push({
                id:data.id,
                pId:data.parentId,
                name:data.name,
                open:true,
                checked:data.isChecked
            });
        }
        return nodes;
    }

    /**
     * 提交保存信息
     */
    function doRoleAddMenus(obj, elem){
        var treeObj=$.fn.zTree.getZTreeObj(obj);
        var nodes = treeObj.getCheckedNodes(true);
        if(nodes.length < 1){
            BJUI.alertmsg('ok', '请勾选菜单');
            return;
        }
        var ids= "";
        for(var i=0;i<nodes.length;i++){
            ids += nodes[i].id + ",";
        }
        elem.val(ids);
        //保存授权
        BJUI.ajax('doajax', {
            url: "/admin/role/roleAddMenu",
            data: $("#role_add_menu_form_id").serialize(),
            loadingmask: true,
            callback: function(response, optiocns) {
                BJUI.dialog('closeCurrent');
                if (response.haveError) {
                    BJUI.alertmsg('error', response.errMsg, {
                        okCall: function () {
                            $('#role_table_cl').datagrid("refresh",true);
                        }
                    });

                } else {
                    BJUI.alertmsg('ok', response.data, {
                        okCall: function () {
                            $('#role_table_cl').datagrid("refresh",true);
                        }
                    });
                }
            }
        });
    }
</script>